package com.hrm.job.dao;

import com.hrm.utils.PageModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class JobProvider {
    //注意有多个参数需要接收时需要在方法参数位置加上对应的@Param注解
    public String selectJob(@Param("name") String name,@Param("pageModel") PageModel pageModel){
        //select * from job_inf where name like '%'#{name}'%' limit #{},#{}
        /*匿名内部类*/

        String sql = new SQL(){
            {
              this.SELECT("*");
              this.FROM("job_inf");
              if(name !=null && !"".equals(name)){
                  this.WHERE("name like '%' #{name} '%'");
              }
               // PageModel pageModel = (PageModel) map.get("pageModel");
                /*mybatis 3.5.2有这个方法，3.4.6没有这个方法可以使用
                * sql =sql +" limit #{pageModel.firstLimitParam},#{pageModel.pageSize}"
                * 注意在limit前加空格
                * */
                this.LIMIT("#{pageModel.firstLimitParam},#{pageModel.pageSize}");
            }

        }.toString();

        return sql;
    }


    /*public String selectJob(Map map){
        //select * from job_inf where name like '%'#{name}'%' limit #{},#{}
        *//*匿名内部类*//*

        String sql = new SQL(){
            {
                this.SELECT("*");
                this.FROM("job_inf");
                if(map.get("name")!=null && !"".equals(map.get("name"))){
                    this.WHERE("name like '%' #{name} '%'");
                }
                // PageModel pageModel = (PageModel) map.get("pageModel");
                *//*mybatis 3.5.2有这个方法，3.4.6没有这个方法可以使用
                 * sql =sql +" limit #{pageModel.firstLimitParam},#{pageModel.pageSize}"
                 * 注意在limit前加空格
                 * *//*
                this.LIMIT("#{pageModel.firstLimitParam},#{pageModel.pageSize}");
            }

        }.toString();

        return sql;
    }*/


    public String selectJobCount(String name){
        //select count(*) from job_inf where name like '%' #{name} '%'
        String sql = new SQL(){
            {
                this.SELECT("count(*)");
                this.FROM("job_inf");

                if(name != null && !"".equals(name)){
                    this.WHERE("name like '%' #{name} '%'");
                }
            }
        }.toString();
        return sql;
    }


    public String deleteJob(@Param("ids") Integer[] ids){
        //delete from job_inf where id in（id,id,id(假设选中三个id)）
        StringBuffer sql = new StringBuffer();
        sql.append("delete from job_inf where id in (");
        for (int id:ids){
            sql.append(id+",");

        }
        sql.deleteCharAt(sql.length()-1);
        sql.append(")");
        return sql.toString();
    }
}
